﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using System.Text;

//zjf@ihangjing.com 2012-5-10
public partial class qy_54tss_Admin_ajax_GetDeliverList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();

        StringBuilder deliver = new StringBuilder("<ul>");

        IList<DeliverInfo> list = new List<DeliverInfo>();

        Deliver dal = new Deliver();
        string where = " delflag=0 and workstate > 0 ";
        int cid = HjNetHelper.GetQueryInt("cid", 0);
        if (cid > 0)
        {
            where += " and inve1 = " + cid + "";
        }

        int CodeId = HjNetHelper.GetQueryInt("CodeId", -1);
        if (CodeId >= 0)
        {
            where += " and CodeId = " + CodeId + "";
        }

        DateTime offlinetimedoor = DateTime.Now.AddMinutes(-1 * Constant.OffLineDoor);

        int Status = HjNetHelper.GetQueryInt("Status", -1);
        if (Status >= 0)
        {
            switch (Status)
            {
                case 0:
                    where += " and ( lasttime <= '" + offlinetimedoor + "' or lasttime is null)";
                    break;
                case 1:
                    where += " and lasttime > '" + offlinetimedoor + "'";
                    break;
            }
        }

        int scope = HjNetHelper.GetQueryInt("scope", 0);
        if (scope > 0 && scope != Constant.allOrderTag)
        {
            where = new RightsTool().getDeliverWhere(where, scope);
        }

        //按照订单数量排序
        list = dal.GetListWithOrderNum(1000, 1, where, "TOrderNum", 1);
        string jsonstr = "[";

        foreach (DeliverInfo info in list)
        {
            string DeliverStatusClass = "";
            string offlineminutes  = "";
            if (info.AddDate <= offlinetimedoor)
            {
                DeliverStatusClass = "offline";
                offlineminutes = "&nbsp;("+Convert.ToInt32((DateTime.Now - info.AddDate).TotalMinutes)+")";
            }
            else
            {
                DeliverStatusClass = WebUtility.GetDeliverStatus(info.workstate);
                
            }
            deliver.Append("<li class=\"" + DeliverStatusClass + "\">");



            deliver.Append("<a href=\"javascript:void(0)\" ondblclick=\"spread2deliver(" + info.DataId + ",'" + info.Name + "')\" onclick=\"showme(" + info.OrderNum.ToString() + " ,'" + info.Name + "' , '" + info.Lat + "','" + info.Lng + "','" + info.DataId + "','" + info.GpsIMEI + "','" + info.Phone + "')\">");
            deliver.Append("<p>" + info.OrderNum.ToString() + offlineminutes+"</p>");
            deliver.Append("<p>" + info.Name + "</p>");
            deliver.Append("</a></li>");
            jsonstr += "{'Name':'" + info.Name + "','OrderNum':'" + info.OrderNum + "','Lat':'" + info.Lat + "','Lng':'" + info.Lng + "','DataId':'" + info.DataId + "','GpsIMEI':'" + info.GpsIMEI + "','Phone':'" + info.Phone + "','workstate':'" + info.workstate + "'},";
        }
        jsonstr = WebUtility.dellast(jsonstr);
        jsonstr += "]";

        deliver.Append("</ul>");
        string hidejson = "<input type='hidden' id='deliverlistjson' value=\"" + jsonstr + "\">";
        deliver.Append(hidejson);

        Response.Write(deliver.ToString());

        Response.End();
    }
}
